//
//  LFBReadBaseView.m
//  ONE
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 GroupNine. All rights reserved.
//

#import "LFBReadBaseView.h"
#import "LFBReadShortModel.h"
#import "LFBReadSerialModel.h"
#import "LFBReadQuestionModel.h"
#import "LFBReadBaseCell.h"
#import <UITableView+FDTemplateLayoutCell.h>

NSString *const kLFBReadBaseViewID = @"LFBReadBaseViewID";

@interface LFBReadBaseView () <UITableViewDelegate, UITableViewDataSource>

/** 底部滚动视图 */
@property (nonatomic, strong) UIScrollView *scrollView;

/** 底部表视图 */
@property (nonatomic, strong) UITableView *tableView;

/** 短篇数据源 */
@property (nonatomic, strong) LFBReadShortModel *readEssay;

/** 连载数据源 */
@property (nonatomic, strong) LFBReadSerialModel *readSerial;

/** 问答数据源 */
@property (nonatomic, strong) LFBReadQuestionModel *readQuestion;

/** 表视图高度约束 */
@property (nonatomic, strong) MASConstraint *tableViewHeightConstraint;

@end

@implementation LFBReadBaseView
{
    /** 表视图行高 */
    NSMutableArray *rowHeights;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createBaseView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createBaseView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self createBaseView];
    }
    return self;
}

#pragma mark --创建BaseView
- (void)createBaseView
{
    if (_scrollView) {
        return;
    }
    
    self.backgroundColor = LYLViewControllerBGColor;
    
    // 初始化滚动视图
    _scrollView = [UIScrollView new];
    _scrollView.backgroundColor = LYLViewControllerBGColor;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    // 自动调整子控件与父控件中间的位置，宽高 保证右边,底部距离不变
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
    [self addSubview:_scrollView];
    [_scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    // 初始化表视图
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(12, 12, SCREEN_WIDTH, 450)];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 注册cell
    [_tableView registerClass:[LFBReadBaseCell class] forCellReuseIdentifier:kLFBReadBaseCellID];
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    _tableView.scrollsToTop = NO;
    _tableView.translatesAutoresizingMaskIntoConstraints = YES;   //自动布局设为NO
    _tableView.layer.masksToBounds = NO;
    _tableView.layer.shadowColor = LYLShadowColor.CGColor;
    _tableView.layer.shadowRadius = 2;
    _tableView.layer.shadowOffset = CGSizeZero;
    _tableView.layer.shadowOpacity = 0.5;
    _tableView.layer.cornerRadius = 5;
    [_scrollView addSubview:_tableView];
    [_tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_scrollView).insets(UIEdgeInsetsMake(12, 12, 12, 12));
        make.width.equalTo(SCREEN_WIDTH - 24);
        _tableViewHeightConstraint = make.height.equalTo(450);
    }];
}

// 设置普通视图
- (void)configureViewWithReadEssay:(LFBReadShortModel *)readEssay readSerial:(LFBReadSerialModel *)readSerial readQuestion:(LFBReadQuestionModel *)readQuestion atIndex:(NSInteger)index
{
    [self configureViewWithReadEssay:readEssay readSerial:readSerial readQuestion:readQuestion atIndex:index inViewController:nil];
}

// 设置带有父视图控制器的视图
- (void)configureViewWithReadEssay:(LFBReadShortModel *)readEssay readSerial:(LFBReadSerialModel *)readSerial readQuestion:(LFBReadQuestionModel *)readQuestion atIndex:(NSInteger)index inViewController:(BaseViewController *)parentViewController
{
    self.viewIndex = index;
    self.parentViewController = parentViewController;
    _readEssay = readEssay;
    _readSerial = readSerial;
    _readQuestion = readQuestion;
    rowHeights = @[@0, @0, @0].mutableCopy;
    _scrollView.contentOffset = CGPointZero;
    [_tableView reloadData];
}

#pragma mark --UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LFBReadBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:kLFBReadBaseCellID forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    
    return cell;
}

// 设置cell
- (void)configureCell:(LFBReadBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [cell configureCellWithReadShortModel:_readEssay];
    }
    else if (indexPath.row == 1)
    {
        [cell configureCellWithReadSerialModel:_readSerial];
    }
    else
    {
        [cell configureCellWithReadQuestionModel:_readQuestion];
    }
}

#pragma mark --UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat rowHeight = [tableView fd_heightForCellWithIdentifier:kLFBReadBaseCellID configuration:^(id cell) {
        [self configureCell:cell atIndexPath:indexPath];
    }];
    rowHeights[indexPath.row] = @(ceil(rowHeight));
    if (indexPath.row == rowHeights.count - 1) {
        NSInteger tableViewHeight = 0;
        for (NSNumber *height in rowHeights) {
            tableViewHeight += [height integerValue];
        }
        _tableViewHeightConstraint.equalTo(tableViewHeight);
    }
    
    return ceil(rowHeight);
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 10)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 10, 0, 10)];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_readSelected) {
        _readSelected(indexPath.row == 0 ? LYLReadTypeShort : (indexPath.row == 1 ? LYLReadTypeSerial : LYLReadTypeQuestion));
    }
}

- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
